home *** CD-ROM | disk | FTP | other *** search
- Path: nntp.teleport.com!nix
- From: nix@teleport.com (Jonathan Nix)
- Newsgroups: comp.lang.c++
- Subject: Re: Need help with strings
- Date: 26 Feb 1996 21:13:44 GMT
- Organization: Teleport - Portland's Public Access (503) 220-1016
- Message-ID: <4gt7q8$co3@maureen.teleport.com>
- References: <312fa506.21210161@news.ucla.edu> <4gpf9b$r7g@news1.usa.pipeline.com>
- NNTP-Posting-Host: linda.teleport.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- : >OK, I just figured out what I did. I had a constructor which was
- : >setting all the pointers to equal 0. I thought that this would
- : >initialize them to point to null but apparently it won't let me assign
- : >anything to the variable later.
- :
- : Phil: I'm afraid you've figured it wrong -- or you're not saying what
- : you mean -- or I misunderstand.
- :
- : First, initializing pointers to 0 does make them to point to what
- : most programmers consider NULL.
- :
- : Second, initializing variables to anything, including 0, does not
- : preclude you from assigning to those variables later. It does,
- : however, mean that you can't strcpy to/from those variables
- : until they have been rebound to point to some valid memory
- : block. But that's different.
-
- A character pointer is just a character pointer, by adding a
- value to the address in increments of characters you get the array of
- characters, or string of characters. If you initialize the character
- pointer to zero you get a character pointer reading ascii 0 when
- dereferenced. Either way, the memory above the pointer is not allocated
- for use unless you specificaly allocate it with the special character
- array initialization syntax..
-
- char *ch = "String of fixed length.\0";
-
- To specify a runtime determined length you use malloc, and to specify
- a compile time constant length you use the array syntax (char ch[10];).
- Either way you cannot run past the length you specified for the string
- without retrieving either a NPA or SIGSEGV error.
-
- There is no built in dynamic input of a character array in any ANSI-C++
- press release that I am aware of. However you can easily write a function
- to return a string of unknown length, by using memory buffers of fixed
- length in the function.
-
- Jonathan Nix
-